<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>医助资料审核</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link href="/node_modules/LobsterUIFrame/layuiadmin/layui/css/layui.css" rel="stylesheet" />
    <link href="/node_modules/LobsterUIFrame/layuiadmin/style/admin.css" rel="stylesheet" />
    <link href="/node_modules/LobsterUIFrame/layuiadmin/layui/css/modules/layui-icon-extend/iconfont.css"
        rel="stylesheet" />
    <script src="/node_modules/LobsterUIFrame/layuiadmin/layui/layui.js"></script>
</head>

<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-input-inline">
                    <label class="layui-form-label">挂号时间：</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input" id="starttime" name="starttime" />
                    </div>
                </div>

                <div class="layui-input-inline">
                    <label class="layui-form-label">患者姓名：</label>
                    <div class="layui-input-block">
                        <input class="layui-input" type="text" autocomplete="off" name="txtName" id="txtName"
                            placeholder="请输入患者姓名" />
                    </div>
                </div>
                <div class="layui-input-inline">
                    <label class="layui-form-label">挂号状态：</label>
                    <div class="layui-input-block">
                        <select id="RegState" name="RegState">
                            <option value="-1">全部</option>
                            <option value="0">未提交</option>
                            <option value="1">已提交</option>
                            <option value="2">已审核</option>
                            <option value="3">已拒绝</option>
                            <option value="7">已退款</option>
                        </select>
                    </div>
                </div>
                <div class="layui-input-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-app-front-search"
                        id="btnSearch">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
                    </button>
                </div>
            </div>

            <div class="layui-card-body">
                <div class="layui-card-body">
                    <table id="datatable" lay-filter="datatable"></table>
                </div>
            </div>
        </div>
    </div>
</body>

</html>
<script>
    layui.config({
        base: '/node_modules/LobsterUIFrame/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    });

    layui.define(['index', 'layer', 'form', 'element', 'table', 'laydate'], function () {
        var layer = layui.layer;
        var form = layui.form;
        var element = layui.element;
        var table = layui.table;
        var $ = layui.$;
        var admin = layui.admin;
        var setter = layui.setter;
        var laydate = layui.laydate;

        var controller = {
            data: {
                datatable: null,
                layerindex: 0,
                search: {
                    sdate: null,
                    edate: null,
                    patname: "",
                    status: -1
                },
            },
            //初始化页面
            initpage: function () {
                this.data.search.sdate = this.getStartTime();
                this.data.search.edate = this.getEndTime();
                this.loadData();
                this.initevent();
            },
            getbaseurl: function (fun) {
                var url = "/lowcode/APP20220607085949108/?fun=" + fun;
                return url;
            },
            openlayer: function (title, elem, successfunction, yesfunction, width, height) {
                var self = this;
                layer.open({
                    type: 2
                    , title: title
                    , content: elem
                    , maxmin: true
                    , area: [width, height]  //宽，高
                    , btn: ['保存', '取消']
                    , success: function (layero, index) {
                        if (successfunction) {
                            successfunction();
                        }
                    }
                    , btn2: function (index, layero) {
                        $("#LAY-app-form-reset").trigger('click');
                    }
                    , cancel: function () {
                        $("#LAY-app-form-reset").trigger('click');
                    }
                    , yes: function (index, layero) {
                        self.data.layerindex = index;
                        getResult((field) => {
                            console.log(field)
                            if (field != null) {
                                layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                                if (yesfunction) {
                                    yesfunction(field);
                                }
                            }
                        })
                    }
                });
            },
            //关闭弹层
            closelayer: function () {
                layer.close(this.data.layerindex);
            },
            loadData() {
                var self = this;
                self.data.search.patname = $("#txtName").val();
                self.data.search.status =$("#RegState").val();
                self.data.datatable = table.render({
                    elem: '#datatable',
                    where: self.data.search,
                    url: self.getbaseurl("assistant.getrecord"),
                    height: 'full-120',
                    cellMinWidth: 80,
                    size: 'sm',
                    page: true,
                    even: true,
                    cols: [[
                        { field: 'PatName', title: '患者姓名', align: 'center', width: 150 },
                        {
                            field: 'Sex', title: '性别', align: 'center', width: 100, templet: res => {
                                if (res.Sex == 1) {
                                    return '男';
                                } else {
                                    return '女';
                                }
                            }
                        },
                        { field: 'Phone', title: '联系电话', align: 'center', width: 150 },
                        {
                            field: 'FurtherDate', title: '复诊日期', align: 'center', width: 180, templet: res => {
                                if (res.FurtherDate)
                                    return res.FurtherDate.substr(0, 10);
                                return "";
                            }
                        },
                        {
                            field: 'TypeName', title: '复诊服务', align: 'center', width: 100, templet: res => {
                                if (res.TypeName) {
                                    return res.TypeName;
                                } else {
                                    return "";
                                }
                            }
                        },
                        { field: 'DoctorName', title: '复诊医生', align: 'center', width: 100 },
                        { field: 'DeptName', title: '所属科室', align: 'center', width: 150 },
                        {
                            field: 'AuditStatus', title: '资料状态', align: 'center', width: 100, templet: res => {
                                if (res.Status == 7) {
                                    return "已退款";
                                } else {
                                    if (res.AuditStatus == 0) {
                                        return "未提交";
                                    } else if (res.AuditStatus == 1) {
                                        return "已提交";
                                    } else if (res.AuditStatus == 2) {
                                        return "已审核";
                                    } else if (res.AuditStatus == 3) {
                                        return "已拒绝";
                                    } else {
                                        return "未提交";
                                    }
                                }

                            }
                        },
                        {
                            field: 'AuditTime', title: '审核时间', align: 'center', width: 180, templet: res => {
                                if (res.AuditTime && res.AuditStatus > 1)
                                    return res.AuditTime.substr(0, 19);
                                return "";
                            }
                        },
                        { title: '操作', fixed: 'right', toolbar: '#operation', width: 200 }
                    ]],
                });
            },
            //初始化事件
            initevent: function () {
                var self = this;
                // 订单查询开始时间
                laydate.render({
                    elem: "#starttime",
                    format: 'yyyy-MM-dd',
                    type: "date",
                    value: this.data.search.sdate + ' ~ ' + this.data.search.edate,
                    range: '~',
                    change: function (value, date, endDate) {
                        $("#starttime").val(value);
                    },
                    done: function (value, date, endDate) {
                        $("#starttime").val(value);
                    }
                });

                form.on('submit(LAY-app-front-search)', data => {
                    var dstr = $("#starttime").val();
                    this.data.search.sdate = dstr.split(" ~ ")[0];
                    this.data.search.edate = dstr.split(" ~ ")[1];
                    this.data.search.status = $("#RegState").val();
                    this.data.search.patname = $("#txtName").val();
                    this.loadData();
                });
                table.on("tool(datatable)", function (obj) {
                    var data = obj.data;//当前行数据
                    var layEvent = obj.event;//获得lay-event的值
                    switch (layEvent) {
                        case 'editExam':
                            self.openeditExam(data)
                            break;
                        case 'detail':
                            self.selDetail(data);
                    }
                })
            },
            openeditExam(data) {
                // var self = this;
                // window.currData = row;
                // self.openlayer("资料审核", 'assistantedit.html', null, function (field) {
                //     self.savedata(field);
                // }, '100%', '100%');

                window.currData = data;
                var self = this;
                layer.open({
                    type: 2
                    , title: '资料审核'
                    , btn: ['确认', '关闭']
                    , content: 'assistantedit.html'
                    , area: ['100%', '100%']  //宽，高
                    , cancel: function (index, layero) {
                        layer.close(index);
                    }
                    , yes: function (index, layero) {
                        var a = getResult();
                        if (!self.checkData(a)) { return; }
                        layer.confirm('确认提交吗?', function (index2) {
                            layer.close(index2);
                            if (a.diagnosisList && a.diagnosisList.length > 0) {
                                a.diagnosisList = JSON.stringify(a.diagnosisList);
                            } else {
                                a.diagnosisList = "";
                            }
                            if (a.medInfo) {
                                a.medInfo = JSON.stringify(a.medInfo);
                            } else {
                                a.medInfo = "";
                            }
                            self.exam(a, () => {
                                self.loadData();
                                layer.close(index); 
                            })
                        })
                    }
                });
            },
            selDetail(data) {
                window.currData = data;
                var self = this;
                var btn = ["关闭"];
                if (data.VisitStatus != 9 && data.AuditStatus == 2) {
                    var btn = ["取消审核", "关闭"];
                }
                layer.open({
                    type: 2
                    , title: '资料详情'
                    , btn: btn
                    , content: 'detail.html'
                    , area: ['100%', '100%']  //宽，高
                    , cancel: function (index, layero) {
                        layer.close(index);
                        console.log(1);
                    }
                    , yes: function (index, layero) {
                        if (btn.length == 2) {
                            var a = { FileId: data.FileId }
                            layer.confirm('确认取消吗?', function (index2) {
                                layer.close(index2);
                                self.cancelAudit(a, () => {
                                    self.loadData();
                                    layer.close(index);
                                })
                            }) 
                        } else {
                            layer.close(index);
                        }

                    }, btn2: function (index, layero) {
                        layer.close(index);

                    }
                });
            },
            /**
             * 验证数据
             * @param {any} data
             */
            checkData: function (data) {
                if (!data) {
                    return false;
                }
                if (data.Status == 0) {
                    layer.msg("请选择审核意见");
                    return false;
                }
                if (data.Status == 3) {
                    if (!data.EditText) {
                        layer.msg("审核不通过请输入原因");
                        return false;
                    }
                }
                return true;
            },
            getEndTime: function () {
                var mdate = new Date();
                var year = mdate.getFullYear();
                var month = mdate.getMonth() + 1;
                if (month < 10)
                    month = "0" + month;
                var ddate = mdate.getDate();
                if (ddate < 10)
                    ddate = "0" + ddate;
                return year + "-" + month + "-" + ddate;
            },
            getStartTime: function () {
                var mdate = new Date();
                var year = mdate.getFullYear();
                var month = mdate.getMonth() + 1;
                if (month < 10)
                    month = "0" + month;
                var ddate = mdate.getDate();
                if (ddate < 10)
                    ddate = "0" + ddate;
                return year + "-" + month + "-" + ddate;
            },
            /****
       * 取消审核
       * */
            cancelAudit: function (a, func) {
                let self = this;
                let url = self.getbaseurl("assistant.cancelAudit");
                admin.req({
                    url: url,
                    method: "POST",
                    data: a,
                    done: res => {
                        if (res.code == 0) {
                            self.loadData();
                            layer.msg("取消成功");
                            func && func();
                        } else {
                            layer.msg(res.msg);
                        }
                    }
                });
            },
            /**
             * 确认审核
             * */
            exam: function (a, func) {
                let self = this;
                let url = self.getbaseurl("assistant.exam");
                admin.req({
                    url: url,
                    method: "POST",
                    data: a,
                    done: res => {
                        if (res.code == 0) {
                            self.loadData();
                            func && func();
                        } else {
                            layer.msg(res.msg);
                        }
                    }
                });
            },

        }

        /*
        * 入口
        */
        controller.initpage();
    }); 
</script>
<script type="text/html" id="operation">
    {{# if( d.AuditStatus=="1" && parseInt(d.Status)<3){ }}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="editExam"><i class="layui-icon layui-icon-edit"></i>资料审核</a>
    {{# } }}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail"><i class="layui-icon layui-icon-search"></i>查看详情</a>
</script>